package com.qdh.service.impl;

import com.qdh.mapper.OrdersMapper;
import com.qdh.mapper.ProductMapper;
import com.qdh.model.Product;
import com.qdh.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<Product> findAll() {
        return productMapper.findAll();

    }

    @Override
    public Product findById(Integer id) {
        return productMapper.findById(id);
    }

    @Override
    public int insertProduct(Product product) {
        return  productMapper.insertProduct(product);
    }

    @Override
    public int updateProduct(Product product) {
        return productMapper.updateProduct(product);
    }

    @Override
    public void deleteProduct(Integer id) {
        if(ordersMapper.countOrders(id)==0){
            productMapper.deleteProduct(id);
        }
    }
}
